/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package da523b;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author linuslofberg
 */
public class Login extends JFrame {

GUI gui;
JTextField usernameText;
JTextField passfield;
JButton loginButton;
JPanel panel;

private final String pass = "81dc9bdb52d04dc20036dbd8313ed055";
    
    public Login() {             
        this.setLocation(500, 300);
        this.setSize(300, 150);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        usernameText = new JTextField("Username");
        passfield = new JPasswordField("Password");
        
        loginButton = new JButton("LOGIN");
        usernameText.setColumns(20);
        passfield.setColumns(20);
        
        panel = new JPanel();
        
        panel.add(usernameText);
        panel.add(passfield);
        panel.add(loginButton);
        
        this.add(panel);
        
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                
                if (usernameText.getText().equals("admin") && md5()) {
                    dispose();
                    gui = new GUI();
                    gui.setVisible(true);
                } else {
                System.out.print("Login failed!");
                }

            }
        });
    }
    
    public boolean md5() {
        String passwordToHash = passfield.getText();
        String generatedPassword = null;
        try {
            // Create MessageDigest instance for MD5
            MessageDigest md = MessageDigest.getInstance("MD5");
            //Add password bytes to digest
            md.update(passwordToHash.getBytes());
            //Get the hash's bytes 
            byte[] bytes = md.digest();
            //This bytes[] has bytes in decimal format;
            //Convert it to hexadecimal format
            StringBuilder sb = new StringBuilder();
            for(int i=0; i< bytes.length ;i++)
            {
                sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            //Get complete hashed password in hex format
            generatedPassword = sb.toString();
            
            if (pass.equals(generatedPassword)) {
                return true;
            } else
                return false;
        } 
        catch (NoSuchAlgorithmException e) 
        {
            e.printStackTrace();
            return false;
        }
    }
    
}